<template>
    <div>
        <div class="operarea">
            <div class="seach">
                <el-input v-model="seachForm.name" :placeholder="$t('name')" clearable size="mini" @keydown.enter.native="seachData"></el-input>
                <el-input v-model="seachForm.description" :placeholder="$t('description')" clearable size="mini" @keydown.enter.native="seachData"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="seachData"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="showAddDlog">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane :label="$t('bigrobe')" name="0">
                <el-card class="cardBox">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item>风格</el-breadcrumb-item>
                        <el-breadcrumb-item>大袍</el-breadcrumb-item>
                    </el-breadcrumb>
                    <el-table style="width: 100%" :data="bigRobeTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                        <el-table-column prop="name" :label="$t('name')">
                        </el-table-column>
                        <el-table-column prop="description" :label="$t('description')">
                        </el-table-column>
                        <el-table-column :label="$t('image')">
                            <template v-slot="scope">
                                <el-image :src="scope.row.imgUrl" class="feelImg" :preview-src-list="getSrcList(scope.$index)"></el-image>
                            </template>
                        </el-table-column>
                        <el-table-column prop="address" width="150" :label="$t('operation')">
                            <template v-slot="scope">
                                <el-button @click.stop="showModify(scope.row)" size="mini">{{$t('redact')}}</el-button>
                                <el-button @click.stop="showDel(scope.row.id)" size="mini">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                        small
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :pager-count="11"
                        :current-page.sync="seachForm.pageNo"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="seachForm.pageSize"
                        layout="sizes, prev, pager, next"
                        :total="total1">
                    </el-pagination>
                </el-card>
            </el-tab-pane>
            <el-tab-pane :label="$t('kaften')" name="1">
                <el-card class="cardBox">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item>风格</el-breadcrumb-item>
                        <el-breadcrumb-item>kaften</el-breadcrumb-item>
                    </el-breadcrumb>
                    <el-table style="width: 100%" :data="kaftenTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                        <el-table-column prop="name" :label="$t('name')">
                        </el-table-column>
                        <el-table-column prop="description" :label="$t('description')">
                        </el-table-column>
                        <el-table-column :label="$t('image')">
                            <template v-slot="scope">
                                <el-image :src="scope.row.imgUrl" class="feelImg" :preview-src-list="getSrcList(scope.$index)"></el-image>
                            </template>
                        </el-table-column>
                        <el-table-column prop="address" width="150" :label="$t('operation')">
                            <template v-slot="scope">
                                <el-button @click.stop="showModify(scope.row)" size="mini">{{$t('redact')}}</el-button>
                                <el-button @click.stop="showDel(scope.row.id)" size="mini">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                        small
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :pager-count="11"
                        :current-page.sync="seachForm.pageNo"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="seachForm.pageSize"
                        layout="sizes, prev, pager, next"
                        :total="total1">
                    </el-pagination>
                </el-card>
            </el-tab-pane>
        </el-tabs>
        <!-- 添加 -->
        <el-dialog :title="$t('add')" :visible.sync="addDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addDlogClose(addDataForm)">
            <el-form :model="addDataForm" :rules="addRules" :ref="addDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="addDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('description')" label-width="70px" prop="description">
                    <el-input type="textarea" autosize placeholder="请输入内容" v-model="addDataForm.description"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="70px" prop="imgUrl">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="handleRemove" :limit="1" :on-exceed="scotland" :file-list="fileStyleList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addData(addDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyDlogClose(modifyDataForm)">
            <el-form :model="modifyDataForm" :rules="modifyRules" :ref="modifyDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="modifyDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('description')" label-width="70px" prop="description">
                    <el-input type="textarea" autosize placeholder="请输入内容" v-model="modifyDataForm.description"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="70px" prop="imgUrl">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="handleRemove" :limit="1" :on-exceed="scotland" :file-list="fileStyleList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyData(modifyDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 删除 -->
        <el-dialog :title="$t('hint')" :visible.sync="delDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    getStyleAll,
    styleAdd,
    deleteStyleById,
} from '@/libs/reqlist.js'
// import Ue from '@/components/ue.vue'
export default {
    data () {
        return {
            total1: 0,
            // 查询条件
            seachForm: {
                type: 0,
                name: '',
                description: '',
                pageNo: 1,
                pageSize: 10
            },
            activeName: '0',
            bigRobeTab: [],
            kaftenTab: [],
            imgList: [],
            doType: 0,
            // 添加
            addDialogVisible: false,
            addDataForm: {
                name: '',
                type: '',
                description: '',
                imgUrl: ''
            },
            addRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                type: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ],
                description: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                imgUrl: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ]
            },
            // 修改
            modifyDialogVisible: false,
            modifyDataForm: {
                id: '',
                name: '',
                type: '',
                description: '',
                imgUrl: ''
            },
            modifyRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                type: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ],
                description: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                imgUrl: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ]
            },
            fileStyleList: [],
            // 删除
            delItemId: 0,
            delDialogVisible: false
        }
    },
    methods: {
        // 选项卡切换
        handleClick(tab) {
            this.seachForm.type = tab.name - 0
            this.getData()
        },
        // 获取表格信息
        getData () {
            this.imgList = []
            getStyleAll(this.seachForm).then(res => {
                if (res.data.code == 0) {
                    res.data.data.data.forEach(item => {
                        this.imgList.push(item.imgUrl)
                    })
                    switch (this.activeName) {
                        case '0':
                            this.bigRobeTab = res.data.data.data
                            break
                        case '1':
                            this.kaftenTab = res.data.data.data
                            break
                    }
                }
            })
        },
        // 条件查询
        seachData () {
            this.seachForm.pageNo = 1
            this.getData()
        },
        handleSizeChange (newSize) {
            this.seachForm.pageSize = newSize
            this.seachData()
        },
        handleCurrentChange (newNum) {
            this.seachForm.pageNo = newNum
            this.seachData()
        },
        getSrcList(index) {
            return this.imgList.slice(index).concat(this.imgList.slice(0, index))
        },
        handleImg(res) {
            if (res.code == 0) {
                switch (this.doType) {
                    case 1:
                        this.addDataForm.imgUrl = res.data
                        break
                    case 2:
                        this.modifyDataForm.imgUrl = res.data
                        break
                }
            }
        },
        handleRemove(file) {
            if (file) {
                switch (this.doType) {
                    case 1:
                        this.fileIndexList = []
                        this.addDataForm.imgUrl = ''
                        break
                    case 2:
                        this.fileIndexList = []
                        this.modifyDataForm.imgUrl = ''
                        break
                }
            }
        },
        // 上传文件超出个数触发
        scotland() {
            this.$message.warning('只能选择一个文件')
        },
        showAddDlog() {
            this.doType = 1
            switch (this.activeName) {
                case '0':
                    this.addDataForm.type = 0
                    break
                case '1':
                    this.addDataForm.type = 1
                    break
            }
            this.addDialogVisible = true
        },
        // 添加
        addData () {
            if (this.addDataForm.name == '') return this.$message.warning('名称不能为空')
            if (this.addDataForm.description == '') return this.$message.warning('描述不能为空')
            if (this.addDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
            styleAdd(JSON.stringify(this.addDataForm)).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('添加成功')
                    this.addDialogVisible = false
                    this.getData()
                }
            })
        },
        addDlogClose (form) {
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
        },
        // 修改
        showModify (data) {
            this.doType = 2
            this.modifyDataForm.id = data.id
            this.modifyDataForm.name = data.name
            this.modifyDataForm.type = data.type
            this.modifyDataForm.imgUrl = data.imgUrl
            this.modifyDataForm.description = data.description
            if (this.modifyDataForm.imgUrl !== '' && this.modifyDataForm.imgUrl !== null) this.fileStyleList.push({ name: data.name, url: this.modifyDataForm.imgUrl })
            this.modifyDialogVisible = true
        },
        modifyData (form) {
            this.$refs[form].validate(vaild => {
                if (!vaild) return
                if (this.modifyDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
                styleAdd(JSON.stringify(this.modifyDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.$message.success('修改成功')
                        this.modifyDialogVisible = false
                        this.getData()
                    }
                })
            })
        },
        modifyDlogClose (form) {
            this.fileStyleList = []
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
        },
        // 删除
        showDel(id) {
            this.delItemId = id
            this.delDialogVisible = true
        },
        delData () {
            deleteStyleById(this.delItemId).then(res => {
                if (res.data.code == 0) {
                    this.$message.success(this.$t('delsuc'))
                    this.delDialogVisible = false
                    this.getData()
                }
            })
        }
    },
    mounted () {
        switch (this.activeName) {
            case 'cblock':
                this.seachForm.type = 0
                break;
            case 'kaften':
                this.seachForm.type = 1
                break;
            case 'suit':
                this.seachForm.type = 2
        }
        this.getData()
    }
}
</script>

<style lang="less" scoped>
@import './stylePro.less';
</style>
